{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Working in a Conda Environment\n",
    "\n",
    "You can choose a [Conda](https://docs.conda.io/projects/conda/en/stable/) environment when you [choose an executor to be used in an electron](choosing_executors.ipynb). This can be useful if you have sandboxed environments for different task types, but need to use the tasks in a workflow, or if the workflow itself uses a different environment.\n",
    "\n",
    "### Prerequisites\n",
    "\n",
    "1. Create one or more Conda environments suitable for running electrons.\n",
    "2. [Start](./covalent_cli.rst) the Covalent services in the current Conda environment.\n",
    "3. Create a second Conda environment in which to execute selected tasks. This environment must have the `cloudpickle` module installed.\n",
    "\n",
    "### Procedure\n",
    "\n",
    "1. Use the `conda_env` keyword argument to assign an executor to a Conda environment.\n",
    "\n",
    "    In the cells below, whenever there is the input parameter `conda_env = \"your_conda_env\"`, replace `\"your_conda_env\"` with the name of the task-execution Conda environment you created, in quotations; for example, `conda_env = \"tensor_flow_environment\"`.\n",
    "\n",
    "    The requested Conda environment must already exist and must have the `cloudpickle` module installed. If it does not, the process fails in the specified environment. However, if `current_env_on_conda_fail` is set to `True`, the dispatcher then runs the electron in the current Conda environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct\n",
    "\n",
    "executor1 = ct.executor.LocalExecutor(\n",
    "    conda_env = \"your_conda_env\",\n",
    "    current_env_on_conda_fail=False,\n",
    ")\n",
    "\n",
    "executor2 = ct.executor.LocalExecutor(\n",
    "    conda_env = \"your_conda_env\",\n",
    "    current_env_on_conda_fail=True,\n",
    ")\n",
    "\n",
    "@ct.electron(executor=executor1)\n",
    "def identity(x):\n",
    "    return x\n",
    "\n",
    "@ct.electron(executor=executor2)\n",
    "def square(x):\n",
    "    return x * x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Run the electrons within a lattice as usual:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "@ct.lattice\n",
    "def workflow(a):\n",
    "    val_1 = identity(x=a)\n",
    "    return square(x=val_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. Dispatch the workflow:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15b34963-8982-4c81-a2e5-15b9d93f0cdf\n"
     ]
    }
   ],
   "source": [
    "dispatch_id = ct.dispatch(workflow)(a=2)\n",
    "print(dispatch_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. Query the workflow result:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Lattice Result\n",
      "==============\n",
      "status: COMPLETED\n",
      "result: 4\n",
      "input args: []\n",
      "input kwargs: {'a': '2'}\n",
      "error: None\n",
      "\n",
      "start_time: 2023-02-01 18:06:00.213197\n",
      "end_time: 2023-02-01 18:06:00.630832\n",
      "\n",
      "results_dir: /Users/mini-me/agnostiq/covalent/doc/source/how_to/execution/results\n",
      "dispatch_id: 15b34963-8982-4c81-a2e5-15b9d93f0cdf\n",
      "\n",
      "Node Outputs\n",
      "------------\n",
      "identity(0): 2\n",
      ":parameter:2(1): 2\n",
      "square(2): 4\n",
      "\n"
     ]
    }
   ],
   "source": [
    "output = ct.get_result(dispatch_id=dispatch_id, wait=True)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### See Also\n",
    "\n",
    "[Getting the Result of a Task (Electron)](../collection/query_electron_execution_result.ipynb)\n",
    "\n",
    "[Getting the Result Previous Workflow Dispatches](../collection/query_lattice_execution_result.ipynb)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
